<template>
  <div class="login-root">
    <el-card>
      <el-form ref="form" class="login-form" :model="submitForm" :rules="rules">
        <el-form-item align="center">
          <h3>登陆</h3>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="submitForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="submitForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button
            v-loading="btnLoading"
            type="primary"
            class="login-button"
            @click.native="handleLoginClick"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      submitForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          {
            required: true, message: '请输入用户名', trigger: 'blur'
          }
        ],
        password: [
          {
            required: true, message: '请输入密码', trigger: 'blur'
          }
        ]
      },
      btnLoading: false
    }
  },
  methods: {
    handleLoginClick() {
      const _this = this
      _this.$refs['form'].validate(valid => {
        if (valid) {
          // TODO login
          this.btnLoading = true
          _this.$notify({
            type: 'success',
            message: '登陆成功',
            duration: 1000,
            onClose() {
              _this.$router.push({
                name: 'HOME'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .login-root {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .login-form {
    width: 400px;
    padding: 12px 24px;
  }

  .login-button {
    width: 100%;
    margin-top: 18px;
  }
</style>
